package cn.com.css.taxInspection.common.job.task;

import cn.com.css.taxInspection.attention.entity.JaajxxYjqlsjEntity;
import cn.com.css.taxInspection.attention.service.JaajxxYjqlsjService;
import cn.com.css.taxInspection.common.constants.Constants;
import cn.com.css.taxInspection.statistics.service.SwjczdhjkzbTaskService;
import cn.com.css.taxInspection.util.TsBqUtils;
import cn.com.css.taxInspection.workrisk.entity.WorkriskRisk1Entity;
import cn.com.css.taxInspection.workrisk.service.WorkriskRisk1Service;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 税务稽查自动化监控指标（2024年8月版） 每天跑
 */
@Component("swjczdhjkzbTask")
public class SwjczdhjkzbTask {
	private Logger logger = LoggerFactory.getLogger(getClass());
	@Resource
	private SwjczdhjkzbTaskService swjczdhjkzbTaskService;


	public void run(String params) {
		logger.info("进入定时任务swjczdhjkzbTask，税务稽查自动化监控指标（2024年8月版）");
		//1.稽查立案后检查任务长期未下达 jclahjcrwcqwxd
		List<Map<String, String>> jclahjcrwcqwxd = swjczdhjkzbTaskService.jclahjcrwcqwxd();
		WorkriskRisk1Entity workriskRisk1Entity;
		for (Map<String,String> jclahjcrwcqwxdMap :jclahjcrwcqwxd) {
			workriskRisk1Entity = new WorkriskRisk1Entity();
			String jcajxxuuid = String.valueOf(jclahjcrwcqwxdMap.get("jcajxxuuid"));
			String jcbmDm = String.valueOf(jclahjcrwcqwxdMap.get("jcbmdm"));
			String xary = String.valueOf(jclahjcrwcqwxdMap.get("aydjr"));
			setWorkRisk1Value(workriskRisk1Entity,jcajxxuuid,jcbmDm,xary,null, null, null,"稽查立案后检查任务长期未下达","用于监控税务机关是否稽查立案后及时下达检查任务" );
			swjczdhjkzbTaskService.save(workriskRisk1Entity);
		}
		//2.调查核实包括协查超期 dchsbkxccq
		List<Map<String, String>> dchsbkxccq = swjczdhjkzbTaskService.dchsbkxccq();
		for (Map<String,String> dchsbkxccqMap :dchsbkxccq) {
			workriskRisk1Entity = new WorkriskRisk1Entity();
			String jcajxxuuid = String.valueOf(dchsbkxccqMap.get("jcajxxuuid"));
			String jcbmDm = String.valueOf(dchsbkxccqMap.get("ayszjgdm"));
			String tzsry = String.valueOf(dchsbkxccqMap.get("aydjr"));
			setWorkRisk1Value(workriskRisk1Entity,jcajxxuuid,jcbmDm,null,null, null, tzsry,"调查核实（包括协查）超期","未在《调查核实（包括协查）任务通知书》要求的日期前提交《调查报告》" );
			swjczdhjkzbTaskService.save(workriskRisk1Entity);
		}
		//3.未按规定程序延长案件办理时限 wagdcxycajblsx
		List<Map<String, String>> wagdcxycajblsx = swjczdhjkzbTaskService.wagdcxycajblsx();
		for (Map<String,String> wagdcxycajblsxMap :wagdcxycajblsx) {
			workriskRisk1Entity = new WorkriskRisk1Entity();
			String jcajxxuuid = String.valueOf(wagdcxycajblsxMap.get("jcajxxuuid"));
			String jcbmDm = String.valueOf(wagdcxycajblsxMap.get("jcbmdm"));
			String jcry = getJcry(String.valueOf(wagdcxycajblsxMap.get("jcry")));
			String slry = getSlry(String.valueOf(wagdcxycajblsxMap.get("slry")));
			setWorkRisk1Value(workriskRisk1Entity,jcajxxuuid,jcbmDm,null,jcry, slry, null,"未按规定程序延长案件办理时限",
					"稽查局未自立案之日起90日内作出行政处理、处罚决定或者无税收违法行为结论又没有申请延期" );
			swjczdhjkzbTaskService.save(workriskRisk1Entity);
		}
		//4.案件办理超期 ajblcq
		List<Map<String, String>> ajblcq = swjczdhjkzbTaskService.ajblcq();
		for (Map<String,String> ajblcqMap :ajblcq) {
			workriskRisk1Entity = new WorkriskRisk1Entity();
			String jcajxxuuid = String.valueOf(ajblcqMap.get("jcajxxuuid"));
			String jcbmDm = String.valueOf(ajblcqMap.get("jcbmdm"));
			String jcry = getJcry(String.valueOf(ajblcqMap.get("jcry")));
			String slry = getSlry(String.valueOf(ajblcqMap.get("slry")));
			setWorkRisk1Value(workriskRisk1Entity,jcajxxuuid,jcbmDm,null,jcry, slry, null,"案件办理超期",
					"特殊情况和不可抗力除外，案件在首次延期结束后仍未作出行政处理、处罚决定或者无税收违法行为结论" );
			swjczdhjkzbTaskService.save(workriskRisk1Entity);
		}
		//5.拟查补税款所属期超出检查所属期 ncbskssqccjcssq
		List<Map<String, String>> ncbskssqccjcssq = swjczdhjkzbTaskService.ncbskssqccjcssq();
		for (Map<String,String> ncbskssqccjcssqMap :ncbskssqccjcssq) {
			workriskRisk1Entity = new WorkriskRisk1Entity();
			String jcajxxuuid = String.valueOf(ncbskssqccjcssqMap.get("jcajxxuuid"));
			String jcbmDm = String.valueOf(ncbskssqccjcssqMap.get("jcbmdm"));
			String jcry = getJcry(String.valueOf(ncbskssqccjcssqMap.get("jcry")));
			String slry = getSlry(String.valueOf(ncbskssqccjcssqMap.get("slry")));
			setWorkRisk1Value(workriskRisk1Entity,jcajxxuuid,jcbmDm,null,jcry, slry, null,"拟查补税款所属期超出检查所属期",
					"检查环节录入拟查补税款所属期超过了检查所属期" );

			swjczdhjkzbTaskService.save(workriskRisk1Entity);
		}
		//6.《税务处理决定情况表》税款所属期超出检查所属期 swcljdqkbskssqccjcssq
		List<Map<String, String>> swcljdqkbskssqccjcssq = swjczdhjkzbTaskService.swcljdqkbskssqccjcssq();
		for (Map<String,String> swcljdqkbskssqccjcssqMap :swcljdqkbskssqccjcssq) {
			workriskRisk1Entity = new WorkriskRisk1Entity();
			String jcajxxuuid = String.valueOf(swcljdqkbskssqccjcssqMap.get("jcajxxuuid"));
			String jcbmDm = String.valueOf(swcljdqkbskssqccjcssqMap.get("jcbmdm"));
			String jcry = getJcry(String.valueOf(swcljdqkbskssqccjcssqMap.get("jcry")));
			String slry = getSlry(String.valueOf(swcljdqkbskssqccjcssqMap.get("slry")));
			setWorkRisk1Value(workriskRisk1Entity,jcajxxuuid,jcbmDm,null,jcry, slry, null,"《税务处理决定情况表》税款所属期超出检查所属期",
					"审理环节录入《税务处理决定情况表》税款所属期超过了检查所属期" );
			swjczdhjkzbTaskService.save(workriskRisk1Entity);
		}
		//7.被扫描后仍未延长案件办理时限 bsmhrwycajblsx
		List<Map<String, String>> bsmhrwycajblsx = swjczdhjkzbTaskService.bsmhrwycajblsx();
		for (Map<String,String>  bsmhrwycajblsxMap :bsmhrwycajblsx) {
			workriskRisk1Entity = new WorkriskRisk1Entity();
			String jcajxxuuid = String.valueOf(bsmhrwycajblsxMap.get("jcajxxuuid"));
			String jcbmDm = String.valueOf(bsmhrwycajblsxMap.get("jcbmdm"));
			String jcry = getJcry(String.valueOf(bsmhrwycajblsxMap.get("jcry")));
			String slry = getSlry(String.valueOf(bsmhrwycajblsxMap.get("slry")));
			setWorkRisk1Value(workriskRisk1Entity,jcajxxuuid,jcbmDm,null,jcry, slry, null,"被扫描后仍未延长案件办理时限",
					"被扫描出“未按规定延长案件办理时限”风险的案件，实际未进行整改" );
			swjczdhjkzbTaskService.save(workriskRisk1Entity);
		}

		logger.info("税务稽查自动化监控指标（2024年8月版）任务结束");
	}

	private void setWorkRisk1Value(WorkriskRisk1Entity workriskRisk1Entity,String jcajxxuuid,String jcbmDm,
								   String xary,String jcry,String slry,String tzsry,String riskName,String riskDetails) {
		workriskRisk1Entity.setId(TsBqUtils.getUUID());
		workriskRisk1Entity.setRelevance(jcajxxuuid);
		workriskRisk1Entity.setRiskType("6");
		workriskRisk1Entity.setRiskName(riskName);

		workriskRisk1Entity.setRiskManJcry(jcry);
		workriskRisk1Entity.setRiskManSlry(slry);
		workriskRisk1Entity.setRiskManXary(xary);
		workriskRisk1Entity.setRiskManTzsry(tzsry);

		workriskRisk1Entity.setDetails(riskDetails);
		workriskRisk1Entity.setTaxorgcode(jcbmDm);
		workriskRisk1Entity.setDeleted(Constants.DATA_NOT_DELETED);
		workriskRisk1Entity.setCreateTime(new Date());
		workriskRisk1Entity.setCreateId("sysJob");
	}
private String getJcry(String jcry){
	List<String> numbers = new ArrayList<>();
	List<String> parts = Arrays.asList(jcry.split("\\|")); // 先按"|"分割字符串

	for (String part : parts) {
		// 对每个部分使用正则表达式匹配数字，并存入numbers列表
		Pattern pattern = Pattern.compile("\\d+");
		Matcher matcher = pattern.matcher(part);
		while (matcher.find()) {
			numbers.add(matcher.group());
		}
	}
	if(numbers.size()>0){
		return numbers.get(0);
	}else {
		return "";
	}

}

	private String getSlry(String slry){
		List<String> numbers = new ArrayList<>();
		List<String> parts = Arrays.asList(slry.split("\\|")); // 先按"|"分割字符串

		for (String part : parts) {
			// 对每个部分使用正则表达式匹配数字，并存入numbers列表
			Pattern pattern = Pattern.compile("\\d+");
			Matcher matcher = pattern.matcher(part);
			while (matcher.find()) {
				numbers.add(matcher.group());
			}
		}
		if(numbers.size()>0){
			return numbers.get(0);
		}else {
			return "";
		}
	}
}
